home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / Filezilla Server / FileZilla_Server-0_9_41.exe / source / interface / misc / SAPrefsDialog.cpp < prev    next >
C/C++ Source or Header  |  2011-11-06  |  15KB  |  611 lines

  1. /*********************************************************************
  2.  
  3.    SAPrefsDialog
  4.  
  5.    Copyright (C) 2000 Smaller Animals Software, Inc.
  6.  
  7.    This software is provided 'as-is', without any express or implied
  8.    warranty.  In no event will the authors be held liable for any damages
  9.    arising from the use of this software.
  10.  
  11.    Permission is granted to anyone to use this software for any purpose,
  12.    including commercial applications, and to alter it and redistribute it
  13.    freely, subject to the following restrictions:
  14.  
  15.    1. The origin of this software must not be misrepresented; you must not
  16.      claim that you wrote the original software. If you use this software
  17.      in a product, an acknowledgment in the product documentation would be
  18.      appreciated but is not required.
  19.  
  20.    2. Altered source versions must be plainly marked as such, and must not be
  21.      misrepresented as being the original software.
  22.  
  23.    3. This notice may not be removed or altered from any source distribution.
  24.  
  25.    http://www.smalleranimals.com
  26.    smallest@smalleranimals.com
  27.  
  28. **********************************************************************/
  29.  
  30. // SAPrefsDialog.cpp : implementation file
  31. //
  32.  
  33. #include "stdafx.h"
  34. #include "../resource.h"
  35. #include "SAPrefsDialog.h"
  36.  
  37. #if defined(_DEBUG) && !defined(MMGR)
  38. #define new DEBUG_NEW
  39. #undef THIS_FILE
  40. static char THIS_FILE[] = __FILE__;
  41. #endif
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CSAPrefsDialog dialog
  45.  
  46.  
  47. CSAPrefsDialog::CSAPrefsDialog(UINT nIDTemplate /*=CSAPrefsDialog::IDD*/, CWnd* pParent /*=NULL*/)
  48.     : CDialog(nIDTemplate, pParent)
  49. {
  50.     //{{AFX_DATA_INIT(CSAPrefsDialog)
  51.     //}}AFX_DATA_INIT
  52.  
  53.    m_iCurPage = -1;
  54.    m_pages.clear();
  55.  
  56.     m_pStartPage = NULL;
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60.  
  61. CSAPrefsDialog::~CSAPrefsDialog()
  62. {
  63.     // clean up
  64.    for (unsigned int i = 0; i < m_pages.size(); i++)
  65.    {
  66.       pageStruct *pPS = m_pages[i];
  67.       delete pPS;
  68.    }
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72.  
  73. void CSAPrefsDialog::DoDataExchange(CDataExchange* pDX)
  74. {
  75.     CDialog::DoDataExchange(pDX);
  76.     //{{AFX_DATA_MAP(CSAPrefsDialog)
  77.     DDX_Control(pDX, IDC_PAGE_TREE, m_pageTree);
  78.     DDX_Control(pDX, IDC_DLG_FRAME, m_boundingFrame);
  79.     //}}AFX_DATA_MAP
  80.     if (GetDlgItem(IDC_CAPTION_BAR))
  81.     {
  82.         DDX_Control(pDX, IDC_CAPTION_BAR, m_captionBar);
  83.     }
  84.  
  85. }
  86.  
  87.  
  88. BEGIN_MESSAGE_MAP(CSAPrefsDialog, CDialog)
  89.     //{{AFX_MSG_MAP(CSAPrefsDialog)
  90.     ON_WM_CREATE()
  91.     ON_NOTIFY(TVN_SELCHANGED, IDC_PAGE_TREE, OnSelchangedPageTree)
  92.     ON_NOTIFY(TVN_GETDISPINFO, IDC_PAGE_TREE, OnGetdispinfoPageTree)
  93.     ON_BN_CLICKED(IDC_PHELP, OnPhelp)
  94.     //}}AFX_MSG_MAP
  95.     ON_MESSAGE(WM_CHANGE_PAGE, OnChangePage)
  96. END_MESSAGE_MAP()
  97.  
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CSAPrefsDialog message handlers
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102.  
  103. BOOL CSAPrefsDialog::PreTranslateMessage(MSG* pMsg) 
  104. {
  105.     ASSERT(pMsg != NULL);
  106.    ASSERT_VALID(this);
  107.    ASSERT(m_hWnd != NULL);
  108.  
  109.     // Don't let CDialog process the Escape key.
  110.     if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_ESCAPE))
  111.     {
  112.     //    return TRUE;
  113.     }
  114.  
  115.     if (CWnd::PreTranslateMessage(pMsg))
  116.       return TRUE;
  117.  
  118.    // don't translate dialog messages when 
  119.    // application is in help mode
  120.    CFrameWnd* pFrameWnd = GetTopLevelFrame();
  121.    if (pFrameWnd != NULL && pFrameWnd->m_bHelpMode)
  122.       return FALSE;
  123.  
  124.    // ensure the dialog messages will not
  125.    // eat frame accelerators
  126.    pFrameWnd = GetParentFrame();
  127.    while (pFrameWnd != NULL)
  128.    {
  129.       if (pFrameWnd->PreTranslateMessage(pMsg))
  130.          return TRUE;
  131.       pFrameWnd = pFrameWnd->GetParentFrame();
  132.    }
  133.  
  134.    return PreTranslateInput(pMsg);
  135.  
  136. }
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139.  
  140. int CSAPrefsDialog::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  141. {
  142.     if (CDialog::OnCreate(lpCreateStruct) == -1)
  143.         return -1;
  144.     
  145.     return 0;
  146. }
  147.  
  148. /////////////////////////////////////////////////////////////////////////////
  149.  
  150. BOOL CSAPrefsDialog::OnInitDialog() 
  151. {
  152.     CDialog::OnInitDialog();
  153.     
  154.     long l = GetWindowLong(m_pageTree.m_hWnd, GWL_STYLE);
  155. #if (_MSC_VER > 1100)
  156.     l = l | TVS_TRACKSELECT ;
  157. #else
  158.     //#define TVS_TRACKSELECT         0x0200
  159.     l = l | 0x0200;
  160. #endif
  161.     SetWindowLong(m_pageTree.m_hWnd, GWL_STYLE, l);
  162.     
  163.     // where will the dlgs live?
  164.     m_boundingFrame.GetWindowRect(m_frameRect);
  165.     ScreenToClient(m_frameRect);
  166.     //   m_frameRect.DeflateRect(2,2);
  167.     
  168.     if (m_csTitle != _T(""))
  169.         SetWindowText(m_csTitle);
  170.     
  171.     // set some styles for the pretty page indicator bar
  172.     if (::IsWindow(m_captionBar.m_hWnd))
  173.     {
  174.         m_captionBar.m_textClr     = ::GetSysColor(COLOR_3DFACE);
  175.         m_captionBar.m_fontWeight  = FW_BOLD;
  176.         m_captionBar.m_fontSize    = 14;
  177.         m_captionBar.m_csFontName  = _T("Verdana");
  178.         m_captionBar.SetConstantText(m_csConstantText);
  179.     }
  180.     
  181.     // fill the tree. we'll create the pages as we need them
  182.     unsigned int i;
  183.     for (i = 0; i <m_pages.size(); i++)
  184.     {
  185.         pageStruct *pPS = m_pages[i];
  186.         ASSERT(pPS);
  187.         ASSERT(pPS->pDlg);
  188.         if (pPS)
  189.         {
  190.             TV_INSERTSTRUCT tvi;
  191.             
  192.             // find this node's parent...
  193.             tvi.hParent = FindHTREEItemForDlg(pPS->pDlgParent);
  194.             
  195.             tvi.hInsertAfter = TVI_LAST;
  196.             tvi.item.cchTextMax = 0;
  197.             tvi.item.pszText = LPSTR_TEXTCALLBACK;
  198.             tvi.item.lParam = (long)pPS;
  199.             tvi.item.mask = TVIF_PARAM | TVIF_TEXT;
  200.             
  201.             HTREEITEM hTree = m_pageTree.InsertItem(&tvi);
  202.             m_pageTree.Expand(tvi.hParent,TVE_EXPAND);
  203.             
  204.             // keep track of the dlg's we've added (for parent selection)
  205.             if (hTree)
  206.             {
  207.                 DWORD dwTree = (DWORD)hTree;
  208.                 m_dlgMap[pPS->pDlg]=dwTree;
  209.             }
  210.         }
  211.     }
  212.  
  213.     for (i = 0; i < m_pages.size(); i++)
  214.         // if we haven't already, Create the dialog
  215.         if (!::IsWindow(m_pages[i]->pDlg->m_hWnd))
  216.         {
  217.             m_pages[i]->pDlg->Create(m_pages[i]->pDlg->GetID(), this);
  218.         }
  219.     
  220.     // start with page 0
  221.     if (m_pStartPage == NULL)
  222.     {
  223.         if (ShowPage(0))
  224.         {
  225.             m_iCurPage = 0;   
  226.         }
  227.     }
  228.     else
  229.     {
  230.         // find start page
  231.         for (i = 0; i < m_pages.size(); i++)
  232.         {
  233.             pageStruct *pPS = m_pages[i];
  234.             ASSERT(pPS);
  235.             if (pPS)
  236.             {
  237.                 ASSERT(pPS->pDlg);
  238.                 if (pPS->pDlg == m_pStartPage)
  239.                 {
  240.                     ShowPage(i);
  241.                     m_iCurPage = i;
  242.                     break;
  243.                 }
  244.             }
  245.         }
  246.     }
  247.     
  248.     return TRUE;  // return TRUE unless you set the focus to a control
  249.     // EXCEPTION: OCX Property Pages should return FALSE
  250. }
  251.  
  252. /////////////////////////////////////////////////////////////////////////////
  253.  
  254. HTREEITEM CSAPrefsDialog::FindHTREEItemForDlg(CSAPrefsSubDlg *pParent)
  255. {
  256.    // if you didn't specify a parent in AddPage(...) , the
  257.    // dialog becomes a root-level entry
  258.     if (pParent==NULL)
  259.     {
  260.         return TVI_ROOT;
  261.     }
  262.    else
  263.    {
  264.       std::map<CSAPrefsSubDlg *, DWORD>::iterator iter=m_dlgMap.find(pParent);
  265.       if (iter!=m_dlgMap.end())
  266.           return (HTREEITEM)iter->second;
  267.       else
  268.       {
  269.          // you have specified a parent that has not 
  270.          // been added to the tree - can't do that.
  271.          ASSERT(FALSE);
  272.          return TVI_ROOT;
  273.       }
  274.    }
  275. }
  276.  
  277. /////////////////////////////////////////////////////////////////////////////
  278.  
  279. LONG_PTR CSAPrefsDialog::OnChangePage(WPARAM u, LPARAM l)
  280. {
  281.    if (ShowPage(u))
  282.    {
  283.       m_iCurPage = u;   
  284.    }
  285.  
  286.     return 0L;
  287. }
  288.  
  289. /////////////////////////////////////////////////////////////////////////////
  290.  
  291. bool CSAPrefsDialog::AddPage(CSAPrefsSubDlg &dlg, LPCTSTR pCaption, CSAPrefsSubDlg* pDlgParent /*=NULL*/)
  292. {
  293.     if (m_hWnd)
  294.     {
  295.         // can't add once the window has been created
  296.         ASSERT(0);
  297.         return false;
  298.     }
  299.  
  300.    pageStruct *pPS = new pageStruct;
  301.    pPS->pDlg = &dlg;
  302.    pPS->id = dlg.GetID();
  303.    pPS->csCaption = pCaption;
  304.    pPS->pDlgParent = pDlgParent;
  305.  
  306.    m_pages.push_back(pPS);
  307.  
  308.    return true;
  309. }
  310.  
  311. /////////////////////////////////////////////////////////////////////////////
  312.  
  313. BOOL CSAPrefsDialog::PreCreateWindow(CREATESTRUCT& cs) 
  314. {
  315.     if (!CWnd::PreCreateWindow(cs))
  316.       return FALSE;
  317.  
  318.    cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS, NULL, NULL, NULL);
  319.    cs.style |= WS_CLIPCHILDREN;
  320.    return TRUE;
  321. }
  322.  
  323. /////////////////////////////////////////////////////////////////////////////
  324.  
  325. bool CSAPrefsDialog::ShowPage(CSAPrefsSubDlg * pPage)
  326. {
  327.    // find that page
  328.     for (unsigned int i = 0; i < m_pages.size(); i++)
  329.     {
  330.         pageStruct *pPS = m_pages[i];
  331.         ASSERT(pPS);
  332.         if (pPS)
  333.         {
  334.             ASSERT(pPS->pDlg);
  335.             if (pPS->pDlg == pPage)
  336.             {
  337.                 ShowPage(i);
  338.                 m_iCurPage = i;
  339.                 return true;
  340.             }
  341.         }
  342.     }
  343.  
  344.    return false;
  345. }
  346.  
  347. /////////////////////////////////////////////////////////////////////////////
  348.  
  349. bool CSAPrefsDialog::ShowPage(int iPage)
  350. {
  351.     if (::IsWindow(m_captionBar.m_hWnd))
  352.    m_captionBar.SetWindowText(_T(""));
  353.  
  354.     // turn off the current page
  355.    if ((m_iCurPage >= 0) && (m_iCurPage < (int)m_pages.size()))
  356.    {
  357.       pageStruct *pPS = m_pages[m_iCurPage];
  358.       ASSERT(pPS);
  359.       if (pPS)
  360.       {
  361.          ASSERT(pPS->pDlg);
  362.          if (pPS->pDlg)
  363.          {
  364.             if (::IsWindow(pPS->pDlg->m_hWnd))
  365.             {
  366.                pPS->pDlg->ShowWindow(SW_HIDE);
  367.             }
  368.          }
  369.       }
  370.       else
  371.       {
  372.          return false;
  373.       }
  374.    }
  375.  
  376.     // show the new one
  377.    if ((iPage >= 0) && (iPage < (int)m_pages.size()))
  378.    {
  379.       pageStruct *pPS = m_pages[iPage];
  380.       ASSERT(pPS);
  381.  
  382.       if (pPS)
  383.       {
  384.          ASSERT(pPS->pDlg);
  385.          if (pPS->pDlg)
  386.          {
  387.  
  388.                // update caption bar
  389.              if (::IsWindow(m_captionBar.m_hWnd))
  390.             m_captionBar.SetWindowText(pPS->csCaption);
  391.  
  392.             // if we haven't already, Create the dialog
  393.             if (!::IsWindow(pPS->pDlg->m_hWnd))
  394.             {
  395.                pPS->pDlg->Create(pPS->pDlg->GetID(), this);
  396.             }
  397.          
  398.             // move, show, focus
  399.             if (::IsWindow(pPS->pDlg->m_hWnd))
  400.             {
  401.                pPS->pDlg->MoveWindow(m_frameRect.left, m_frameRect.top, m_frameRect.Width(), m_frameRect.Height());
  402.                pPS->pDlg->ShowWindow(SW_SHOW);
  403.                //pPS->pDlg->SetFocus();
  404.             }
  405.  
  406.             // change the tree
  407.  
  408.             // find this in our map
  409.             HTREEITEM hItem = FindHTREEItemForDlg(pPS->pDlg);
  410.             if (hItem)
  411.             {
  412.                // select it
  413.                m_pageTree.SelectItem(hItem);
  414.             }
  415.             return true;
  416.          }
  417.       }
  418.    }
  419.  
  420.    return false;
  421. }
  422.  
  423. /////////////////////////////////////////////////////////////////////////////
  424.  
  425. void CSAPrefsDialog::OnOK() 
  426. {
  427.    // if EndOK returns true, all of the UpdateData(TRUE)'s succeeded
  428.    if (EndOK())
  429.    {
  430.        CDialog::OnOK();
  431.    }
  432. }
  433.  
  434. /////////////////////////////////////////////////////////////////////////////
  435.  
  436. bool CSAPrefsDialog::EndOK()
  437. {
  438.    bool bOK = true;
  439.  
  440.    CSAPrefsSubDlg * pPage = NULL;
  441.  
  442.     // first, UpdateData...
  443.    unsigned int i;
  444.    for (i = 0; i < m_pages.size(); i++)
  445.    {
  446.       pageStruct *pPS = m_pages[i];
  447.       ASSERT(pPS);
  448.       if (pPS)
  449.       {
  450.          ASSERT(pPS->pDlg);
  451.          if (pPS->pDlg)
  452.          {
  453.             if (::IsWindow(pPS->pDlg->m_hWnd))
  454.             {
  455.                if (!pPS->pDlg->UpdateData(TRUE))
  456.                {
  457.                   bOK = false;
  458.                   pPage = pPS->pDlg;
  459.                   break;
  460.                }
  461.             }
  462.          }
  463.       }
  464.    }
  465.  
  466.    // were there any UpdateData errors?
  467.    if ((!bOK) && (pPage!=NULL))
  468.    {
  469.       ShowPage(pPage);
  470.       return false;
  471.    }
  472.  
  473.    // tell all of the sub-dialogs "OK"
  474.    for ( i = 0; i < m_pages.size(); i++)
  475.    {
  476.       pageStruct *pPS = m_pages[i];
  477.       ASSERT(pPS);
  478.       if (pPS)
  479.       {
  480.          ASSERT(pPS->pDlg);
  481.          if (pPS->pDlg)
  482.          {
  483.             if (::IsWindow(pPS->pDlg->m_hWnd))
  484.             {
  485.                pPS->pDlg->OnOK();
  486.             }
  487.          }
  488.       }
  489.    }
  490.  
  491.    return true;
  492. }
  493.  
  494. /////////////////////////////////////////////////////////////////////////////
  495.  
  496. void CSAPrefsDialog::OnCancel() 
  497. {
  498.     // tell all of the sub-dialogs "Cancel"
  499.    for (unsigned int i = 0; i < m_pages.size(); i++)
  500.    {
  501.       pageStruct *pPS = m_pages[i];
  502.       ASSERT(pPS);
  503.       
  504.       if (pPS)
  505.       {
  506.          ASSERT(pPS->pDlg);
  507.          if (pPS->pDlg)
  508.          {
  509.             if (::IsWindow(pPS->pDlg->m_hWnd))
  510.             {
  511.                pPS->pDlg->OnCancel();
  512.             }
  513.          }
  514.       }
  515.    }
  516.  
  517.     CDialog::OnCancel();
  518. }
  519.  
  520.  
  521. /////////////////////////////////////////////////////////////////////////////
  522.  
  523. void CSAPrefsDialog::EndSpecial(UINT res, bool bOk)
  524. {
  525.    if (bOk)
  526.    {
  527.       EndOK();
  528.    }
  529.  
  530.     EndDialog(res);
  531. }
  532.  
  533. /////////////////////////////////////////////////////////////////////////////
  534.  
  535. void CSAPrefsDialog::OnSelchangedPageTree(NMHDR* pNMHDR, LRESULT* pResult) 
  536. {
  537.     NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  538.  
  539.    if (pNMTreeView->itemNew.lParam)
  540.    {
  541.         // find out which page was selected
  542.       int iIdx = -1;
  543.       for (unsigned int i = 0; i < m_pages.size(); i++)
  544.       {
  545.          if (m_pages[i]==(pageStruct *)pNMTreeView->itemNew.lParam)
  546.          {
  547.             iIdx = i;
  548.             break;
  549.          }
  550.       }
  551.  
  552.         // show that page
  553.       if ((iIdx >= 0) && (iIdx < (int)m_pages.size()))
  554.       {
  555.          if (m_iCurPage!=iIdx)
  556.          {
  557.             PostMessage(WM_CHANGE_PAGE, iIdx);
  558.          }
  559.       }
  560.    }
  561.  
  562.     *pResult = 0;
  563. }
  564.  
  565. /////////////////////////////////////////////////////////////////////////////
  566.  
  567. void CSAPrefsDialog::OnGetdispinfoPageTree(NMHDR* pNMHDR, LRESULT* pResult) 
  568. {
  569.     TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
  570.  
  571.     // return the caption of the appropriate dialog
  572.    if (pTVDispInfo->item.lParam)
  573.    {
  574.       if (pTVDispInfo->item.mask & TVIF_TEXT)
  575.       {
  576.          pageStruct *pPS = (pageStruct *)pTVDispInfo->item.lParam;
  577.          _tcscpy(pTVDispInfo->item.pszText, pPS->csCaption);
  578.       }
  579.    }
  580.    
  581.     *pResult = 0;
  582. }
  583.  
  584. /////////////////////////////////////////////////////////////////////////////
  585.  
  586. void CSAPrefsDialog::OnPhelp() 
  587. {
  588.     // simulate the property sheet method of sending Help (with WM_NOTIFY)
  589.    if ((m_iCurPage >= 0) && (m_iCurPage < (int)m_pages.size()))
  590.    {
  591.       pageStruct *pPS = m_pages[m_iCurPage];
  592.       ASSERT(pPS);
  593.       ASSERT(pPS->pDlg);
  594.       if (pPS)
  595.       {
  596.          if (pPS->pDlg)
  597.          {
  598.             if (::IsWindow(pPS->pDlg->m_hWnd))
  599.             {
  600.                // help!
  601.                   NMHDR nm;
  602.                   nm.code=PSN_HELP;
  603.                   nm.hwndFrom=m_hWnd;
  604.                   nm.idFrom=CSAPrefsDialog::IDD;
  605.                   pPS->pDlg->SendMessage(WM_NOTIFY, 0, (long)&nm);
  606.             }
  607.          }
  608.         }
  609.     }
  610. }
  611.